package cn.iocoder.yudao.module.design.controller.admin.problemrecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.problemrecord.vo.*;
import cn.iocoder.yudao.module.design.convert.problemrecord.ProblemRecordConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.problemrecord.ProblemRecordDO;
import cn.iocoder.yudao.module.design.service.problemrecord.ProblemRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 问题记录")
@RestController
@RequestMapping("/design/problem-record")
@Validated
public class ProblemRecordController {

    @Resource
    private ProblemRecordService problemRecordService;

    @PostMapping("/create")
    @Operation(summary ="创建问题记录")
    @PreAuthorize("@ss.hasPermission('design:problem-record:create')")
    public CommonResult<Long> createProblemRecord(@Valid @RequestBody ProblemRecordCreateReqVO createReqVO) {
        return success(problemRecordService.createProblemRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新问题记录")
    @PreAuthorize("@ss.hasPermission('design:problem-record:update')")
    public CommonResult<Boolean> updateProblemRecord(@Valid @RequestBody ProblemRecordUpdateReqVO updateReqVO) {
        problemRecordService.updateProblemRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除问题记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:problem-record:delete')")
    public CommonResult<Boolean> deleteProblemRecord(@RequestParam("id") Long id) {
        problemRecordService.deleteProblemRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得问题记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:problem-record:query')")
    public CommonResult<ProblemRecordRespVO> getProblemRecord(@RequestParam("id") Long id) {
        ProblemRecordDO problemRecord = problemRecordService.getProblemRecord(id);
        return success(ProblemRecordConvert.INSTANCE.convert(problemRecord));
    }

    @GetMapping("/getProblemRecordData")
    @Operation(summary ="获得问题记录详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:problem-record:query')")
    public CommonResult<ProblemRecordRespVO> getProblemRecordData(@RequestParam("id") Long id) {

        return success(problemRecordService.getProblemRecordData(id));
    }

    @GetMapping("/list")
    @Operation(summary ="获得问题记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:problem-record:query')")
    public CommonResult<List<ProblemRecordRespVO>> getProblemRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ProblemRecordDO> list = problemRecordService.getProblemRecordList(ids);
        return success(ProblemRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得问题记录分页")
    @PreAuthorize("@ss.hasPermission('design:problem-record:query')")
    public CommonResult<PageResult<ProblemRecordRespVO>> getProblemRecordPage(@Valid ProblemRecordPageReqVO pageVO) {
        PageResult<ProblemRecordDO> pageResult = problemRecordService.getProblemRecordPage(pageVO);
        return success(ProblemRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出问题记录 Excel")
    @PreAuthorize("@ss.hasPermission('design:problem-record:export')")
   
    public void exportProblemRecordExcel(@Valid ProblemRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProblemRecordDO> list = problemRecordService.getProblemRecordList(exportReqVO);
        // 导出 Excel
        List<ProblemRecordExcelVO> datas = ProblemRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问题记录.xls", "数据", ProblemRecordExcelVO.class, datas);
    }

}
